package com.etoak.stock.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.etoak.stock.mapper.SysPermissionMapper;
import com.etoak.stock.mapper.SysRoleMapper;
import com.etoak.stock.mapper.SysUserRoleMapper;
import com.etoak.stock.pojo.domin.MenuPermissionsDomain;
import com.etoak.stock.pojo.domin.SysUserRoleDomain;
import com.etoak.stock.pojo.entity.SysPermission;
import com.etoak.stock.pojo.entity.SysRole;
import com.etoak.stock.service.UserRoleService;
import com.etoak.stock.vo.req.updatePermsByRoleIdReqVo;
import com.etoak.stock.vo.resp.PageResult;
import com.etoak.stock.vo.resp.R;
import com.etoak.stock.vo.resp.ResponseCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author by 孙有泽
 * @Date 2024/10/26 14:43
 * @Description
 */
@Service
@Slf4j
public class UserRoleServiceImpl implements UserRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysPermissionMapper sysPermissionMapper;


    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    /*
     * @author 孙有泽
     * @date 2024/10/26 15:02
     * @description 分页查询当前角色信息
     */
    @Override
    public R<PageResult<SysUserRoleDomain>> getRoles(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUserRoleDomain> allRoles=sysRoleMapper.getRoles();
        if(CollectionUtil.isEmpty(allRoles)){
            return R.error(ResponseCode.NO_RESPONSE_DATA.getMessage());
        }
        PageInfo<SysUserRoleDomain> sysUserRoleDomainPageInfo = new PageInfo<>(allRoles);
        PageResult<SysUserRoleDomain> sysUserRoleDomainPageResult = new PageResult<>(sysUserRoleDomainPageInfo);
        return R.ok(sysUserRoleDomainPageResult);

    }

    @Override
    public R<List<MenuPermissionsDomain>> getTree() {
        List<SysPermission> sysPermissions=sysPermissionMapper.selectAll();
        if(CollectionUtil.isEmpty(sysPermissions)){
            return R.error(ResponseCode.NO_RESPONSE_DATA.getMessage());
        }
        List<MenuPermissionsDomain> menuPermissionsDomains = diGuiTree(sysPermissions, 0L);
        return R.ok(menuPermissionsDomains);
    }
    /*
     * @author 孙有泽
     * @date 2024/11/13 13:32
     * @description 更新用户的状态信息
     */
    @Override
    public R updateStatus(Long roleId, Integer status) {
        int i=sysRoleMapper.updateRoleStatus(roleId,status);
        if(i>0){
            return R.ok();
        }else {
            return R.error(ResponseCode.DATA_ERROR.getMessage());
        }
    }

    @Override
    public R deleteRole(Long roleId) {

        this.sysUserRoleMapper.deleteUserRoleByRoleId(roleId);
        int i=this.sysRoleMapper.deleteRole(roleId);
        if(i>0){
            return R.ok();
        }else
            return R.error(ResponseCode.DATA_ERROR.getMessage());
    }
    /*
     * @author 孙有泽
     * @date 2024/11/14 13:57
     * @description 添加角色和角色关联权限
     */

    @Override
    public R updatePermsByRoles(updatePermsByRoleIdReqVo updatePermsByRoleIdReqVo) {
        return null;
    }

    public static List<MenuPermissionsDomain> diGuiTree(List<SysPermission> sysPermissions,Long pid){
        List<MenuPermissionsDomain> menuPermissionsDomains = new ArrayList<>();
        for (SysPermission sysPermission : sysPermissions) {
            if(sysPermission.getPid().equals(pid)){
                if(sysPermission.getType().intValue()!=3){
                    menuPermissionsDomains.add(new MenuPermissionsDomain(sysPermission.getId(),sysPermission.getTitle(),sysPermission.getIcon(),
                            sysPermission.getUrl() ,sysPermission.getName(),
                            diGuiTree(sysPermissions,sysPermission.getId())
                    ));
                }
            }
        }
        return menuPermissionsDomains;
    }
}
